/* 获取昨日*/
function getYesterdayDateTime() {
  const today = new Date();
  const yesterday = new Date(today);
  yesterday.setDate(yesterday.getDate() - 1);

  function padZero(num) {
    return num < 10 ? '0' + num : num;
  }

  const year = yesterday.getFullYear();
  const month = padZero(yesterday.getMonth() + 1);
  const day = padZero(yesterday.getDate());

  const startTime = year + '-' + month + '-' + day;
  const endTime = year + '-' + month + '-' + day;

  return {startTime, endTime};
}

/* 获取本周时间 */
function getThisWeekDates() {
  // 获取本周的开始时间和结束时间
  const today = new Date()

  // 计算今天是一周的第几天
  const dayOfWeek = today.getDay(); // 星期天是0，星期一是1

  // 往前推一天到周日开始（如果是周一，则不需要）
  let weekStart = new Date(today);
  if (dayOfWeek !== 0) {
    weekStart.setDate(weekStart.getDate() - dayOfWeek + 1);
  } else {
    weekStart.setDate(weekStart.getDate() - dayOfWeek - 6);
  }

  // 往后推一天到周日结束
  let weekEnd = new Date(today);
  if (dayOfWeek !== 0) {
    weekEnd.setDate(weekEnd.getDate() + (7 - dayOfWeek));
  } else {
    weekEnd.setDate(weekEnd.getDate());
  }
  const start = `${weekStart.getFullYear()}-${(weekStart.getMonth() + 1) > 9 ? weekStart.getMonth() + 1 : '0' + (weekStart.getMonth() + 1)}-${weekStart.getDate() > 9 ? weekStart.getDate() : '0' + weekStart.getDate()}`
  const end = `${weekEnd.getFullYear()}-${(weekEnd.getMonth() + 1) > 9 ? weekEnd.getMonth() + 1 : '0' + (weekEnd.getMonth() + 1)}-${weekEnd.getDate() > 9 ? weekEnd.getDate() : '0' + weekEnd.getDate()}`

  return {
    start: start, end: end
  }

}

/* 获取上周时间 */
function getLastWeekDates() {
  const now = new Date();
  const startOfThisWeek = new Date(now.getFullYear(), now.getMonth(), now.getDate() - now.getDay() + 1); // 本周开始日期
  const startOfLastWeek = new Date(startOfThisWeek);
  const endOfLastWeek = new Date(startOfThisWeek);

  // 上周的开始时间是本周开始时间减去7天
  startOfLastWeek.setDate(startOfLastWeek.getDate() - 7);
  // 上周的结束时间是本周开始时间减去1天
  endOfLastWeek.setDate(endOfLastWeek.getDate() - 1);

  // 格式化日期为年-月-日
  function formatDate(date) {
    const year = date.getFullYear();
    const month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的
    const day = ('0' + date.getDate()).slice(-2);
    return `${year}-${month}-${day}`;
  }

  return {
    startOfLastWeek: formatDate(startOfLastWeek),
    endOfLastWeek: formatDate(endOfLastWeek)
  };
}

/*获取本月时间 */
function getThisMonthStartAndEnd() {
  const now = new Date();
  const year = now.getFullYear();
  const month = ('0' + (now.getMonth() + 1)).slice(-2); // 确保月份是两位数
  const startDate = `${year}-${month}-01`; // 本月开始时间
  const endDate = `${year}-${month}-${new Date(year, month, 0).getDate()}`; // 本月结束时间

  return {
    startTime: startDate,
    endTime: endDate
  };
}

/*获取上月时间 */
function getLastMonthStartAndEnd() {
  const today = new Date();
  const thisMonth = today.getMonth();
  const nextMonth = thisMonth % 12;
  const lastMonth = thisMonth === 0 ? 11 : thisMonth - 1;

  const lastMonthFirstDay = new Date(today.getFullYear(), lastMonth, 1);
  const lastMonthLastDay = new Date(today.getFullYear(), nextMonth, 0);

  function pad(number) {
    return number < 10 ? '0' + number : number;
  }

  function formatDate(date) {
    return date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate());
  }

  return {
    start: formatDate(lastMonthFirstDay),
    end: formatDate(lastMonthLastDay)
  };
}

export {
  getYesterdayDateTime,
  getThisWeekDates,
  getLastWeekDates,
  getThisMonthStartAndEnd,
  getLastMonthStartAndEnd,
}
